package com.lt.flashcard.view;

import org.apache.commons.lang3.StringUtils;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.metainfo.ComponentInfo;
import org.zkoss.zk.ui.select.SelectorComposer;

import com.lt.flashcard.Utility;
import com.lt.flashcard.model.entity.Cart;
import com.lt.flashcard.model.entity.Orderdetail;
import com.lt.flashcard.model.service.AAService;
import com.lt.flashcard.model.service.EcommerceService;
import com.lt.flashcard.model.service.Repository;

public class CartFinishCtrl extends SelectorComposer<Component> {

	private static final long serialVersionUID = 1L;
	
	private Orderdetail order;

	@Override
	public ComponentInfo doBeforeCompose(Page page, Component parent, ComponentInfo compInfo) {
		AAService asrv = Repository.getInstance().getAAService();
		if(!asrv.isLogin()) {
			Utility.loginOrAccessDenied(false);
			return null;
		}
		
		String id = Executions.getCurrent().getParameter("id");
		if(StringUtils.isBlank(id)) {
			Utility.displayAccessDenied(false);
			return null;
		}
		
		Long lid = Long.parseLong(id);
		EcommerceService esrv = Repository.getInstance().getEcommerceService();
		order = esrv.getOrderdetail(lid);
		if(order == null) {
			Utility.displayAccessDenied(false);
			return null;
		}
		
		return super.doBeforeCompose(page, parent, compInfo);
	}

	public Orderdetail getOrder() {
		return order;
	}

	public void setOrder(Orderdetail order) {
		this.order = order;
	}
	
	/**
	 * Get the card
	 * @return
	 */
	public Cart getCart() {
		EcommerceService srv = Repository.getInstance().getEcommerceService();
		return srv.getCurrentCart();
	}
}
